package com.syni.mdd.yhd.common.all.quartz.groupBuy;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.service.BmsDxGroupBuyService;
import com.syni.mdd.yhd.common.all.service.groupBuy.BmsDxGroupBuyRecordService;
import com.syni.mdd.yhd.common.all.utils.OrderUtils;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.*;

/**
 * @className BmsVideoQuartz
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/26 16:15
 * @VERSION 1.0
 **/
//@Component
@EnableScheduling
public class BmsGroupBuyQuartz {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsDxGroupBuyService bmsDxGroupBuyService;

    @Autowired
    BmsDxGroupBuyRecordService bmsDxGroupBuyRecordService;

    //@Scheduled(cron = "0 0 0 * * ?")  //每天晚上凌晨执行一次定时任务
    //@Scheduled(cron = "0/5 * * * * ?")
    public void resetVideoNoRandom() {
        OrderUtils.groupByNo.clear();
        log.info("...清楚完毕.....");
        log.info("groupByNo：" + OrderUtils.groupByNo.size());
    }

    @Scheduled(cron = "0 0 0/2 * * ?")
    //@Scheduled(cron = "0/5 * * * * ?")
    public void updateGroupBuyQuartz() {
        final int TIME_END_OFFLINE = BmsDxGroupBuyService.TIME_END_OFFLINE;
        final int SELL_END_OFFLINE = BmsDxGroupBuyService.SELL_END_OFFLINE;
        Date nowTime = new Date();
        //卖光的自动下线
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("isDelete", Constants.COMMON_FLAG_FALSE_INT);
        conditionMap.put("status", Constants.ONLINE_STATUS);
        conditionMap.put("sellInventory", Constants.COMMON_FLAG_FALSE_INT);
        offlineGroupBuy(conditionMap,SELL_END_OFFLINE);
        //特特乐卖光的也自动下线

        //过期的自动下线(普通团购)
        conditionMap.remove("sellInventory");
        Date expireTime = DateTimeUtils.addDay(nowTime,-1);
        conditionMap.put("expireTime_LTTE",expireTime);
        Set<Integer> groupTypes = new HashSet<>();
        groupTypes.add(BmsDxGroupBuyService.GROUP_TYPE_GEN);
        groupTypes.add(BmsDxGroupBuyService.GROUP_TYPE_SECONDS_KILL);
        conditionMap.put("groupType_IN",groupTypes);
        offlineGroupBuy(conditionMap,TIME_END_OFFLINE);

        //如果是限时抢购到期了也自动下线
        conditionMap.clear();
        conditionMap.put("isDelete", Constants.COMMON_FLAG_FALSE_INT);
        conditionMap.put("status", Constants.ONLINE_STATUS);
        conditionMap.put("isTimeLimit",Constants.COMMON_FLAG_TRUE_INT);
        offlineTimeLimitGroupBuy(conditionMap);

    }

    private void offlineTimeLimitGroupBuy(Map<String, Object> conditionMap) {
        Date nowTime = new Date();
        List<BmsDxGroupBuy> bmsDxGroupBuys = bmsDxGroupBuyService.getBaseDao().findAll(new SpecificationUtils(conditionMap));
        List<BmsDxGroupBuy> newBmsDxGroupBuys = new LinkedList<>();
        if(bmsDxGroupBuys != null && bmsDxGroupBuys.size() > 0){
            for(BmsDxGroupBuy bmsDxGroupBuy : bmsDxGroupBuys){
                Date limitBuyTime = bmsDxGroupBuy.getLimitBuyTime();
                if(limitBuyTime != null) {
                    if (DateTimeUtils.match(nowTime,bmsDxGroupBuy.getLimitBuyTime(),DateTimeUtils.MATCH_MODEL_GT)) {
                        bmsDxGroupBuy.setStatus(Constants.ROLLED_ONLINE_STATUS);
                        bmsDxGroupBuyRecordService.addGroupBuyRecord(bmsDxGroupBuy, BmsDxGroupBuyRecordService.RECORD_OFFLINE);
                        /*BeanUtils.copyProperties(bmsDxGroupBuy, newBmsDxGroupBuy);
                        newBmsDxGroupBuy.setStatus(Constants.ROLLED_ONLINE_STATUS);
                        newBmsDxGroupBuy.setId(null);
                        newBmsDxGroupBuy.setFromOrigin(BmsDxGroupBuyService.TIME_END_OFFLINE);
                        newBmsDxGroupBuy.setOfflineTime(new Date());
                        newBmsDxGroupBuys.add(newBmsDxGroupBuy);*/
                    }
                }
            }
            bmsDxGroupBuyService.getBaseDao().saveAll(bmsDxGroupBuys);
            //bmsDxGroupBuyService.getBaseDao().saveAll(newBmsDxGroupBuys);
        }
    }

    public void offlineGroupBuy(Map<String,Object> conditionMap,Integer offlineStatus){
        List<BmsDxGroupBuy> bmsDxGroupBuys = bmsDxGroupBuyService.getBaseDao().findAll(new SpecificationUtils(conditionMap));
        List<BmsDxGroupBuy> newBmsDxGroupBuys = new LinkedList<>();
        if(bmsDxGroupBuys != null && bmsDxGroupBuys.size() > 0){
            for(BmsDxGroupBuy bmsDxGroupBuy : bmsDxGroupBuys){
                BmsDxGroupBuy newBmsDxGroupBuy = new BmsDxGroupBuy();
                bmsDxGroupBuy.setStatus(Constants.ROLLED_ONLINE_STATUS);
                bmsDxGroupBuyRecordService.addGroupBuyRecord(bmsDxGroupBuy, BmsDxGroupBuyRecordService.RECORD_OFFLINE);
                /*BeanUtils.copyProperties(bmsDxGroupBuy,newBmsDxGroupBuy);
                newBmsDxGroupBuy.setStatus(Constants.ROLLED_ONLINE_STATUS);
                newBmsDxGroupBuy.setId(null);
                newBmsDxGroupBuy.setFromOrigin(offlineStatus);
                newBmsDxGroupBuy.setOfflineTime(new Date());
                newBmsDxGroupBuys.add(newBmsDxGroupBuy);*/
            }
            bmsDxGroupBuyService.getBaseDao().saveAll(bmsDxGroupBuys);
            //bmsDxGroupBuyService.getBaseDao().saveAll(newBmsDxGroupBuys);
        }
    }

}
